package mpc;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ControlPanel extends JPanel implements ActionListener{
	public static JButton restart, reset, stepToEnd;
	public static JRadioButton b1, b2, b3, b4;
	ControlPanel(){
		
		this.setLayout(new GridLayout(0, 1));
		
		restart = new JButton("Начать проход заново");
		restart.setActionCommand("restart");
		
		reset = new JButton("Перезагрузить");
		reset.setActionCommand("reset");
		
		stepToEnd = new JButton("Пройти до конца");
		stepToEnd.setActionCommand("stepToEnd");
		stepToEnd.setEnabled(false);
		
		b1 = new JRadioButton("Выбор старта");
		b1.setActionCommand("SetStart");
		b1.setSelected(true);
		GUIClass.pan.setMode(new SettingStartMode());
		
		b2 = new JRadioButton("Расстановка кирпичей");
		b2.setActionCommand("Brick");
		
		b3 = new JRadioButton("Трассировка");
		b3.setEnabled(false);
		b3.setActionCommand("Step");
		
		b4 = new JRadioButton("Прокладка пути");
		b4.setActionCommand("tracePath");
		
		ButtonGroup group = new ButtonGroup();
		group.add(b1);
		group.add(b2);
		group.add(b3);
		group.add(b4);
		
		restart.addActionListener(this);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		stepToEnd.addActionListener(this);
		b4.addActionListener(this);
		reset.addActionListener(this);
		
		this.add(restart); this.add(b1); this.add(b2); this.add(b3); this.add(stepToEnd); this.add(b4); this.add(reset);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//IMode mode;
		String com = e.getActionCommand();
		
		if(com == "SetStart"){
			//mode = new SetStartMode();
			GUIClass.pan.setMode(new SettingStartMode());
		} else if(com == "Brick"){
			GUIClass.pan.setMode(new SettingBrickMode());
		} else if(com == "Step"){
			GUIClass.pan.setMode(new SteppingMode());
			b1.setEnabled(false);
			b2.setEnabled(false);
		} else if (com == "stepToEnd") {
			boolean t = true;
			while(t) t = GUIClass.VFIELD.doNextStep();
			GUIClass.pan.repaint();
		} else if(com == "restart"){
			GUIClass.VFIELD.restart();
			GUIClass.pan.repaint();
			b1.setEnabled(true);
			b1.setSelected(true);
			b2.setEnabled(true);
		} else if(com == "tracePath"){
			GUIClass.pan.setMode(new FindPathMode());
		} else if(com == "reset"){
			
			String[] options = {"16\u00d716", "10\u00d710", "5\u00d75"};
			String s = (String) JOptionPane.showInputDialog(GUIClass.frame,
					"Выберите размеры поля:",
					"Перезапуск...",
					JOptionPane.PLAIN_MESSAGE,
					null, options, options[0]);
			
			if(s != null){
				int width = 0, height = 0;
				if(s == options[0]){
					width = height = 16;
				} else if(s == options[1]){
					width = height = 10;
				} else if(s == options[2]){
					width = height = 5;
				}
				GUIClass.VFIELD.reset(width, height);
				GUIClass.frame.setSize(width * TableDrawer.SQ_SIZE + GUIClass.dWidth, height * TableDrawer.SQ_SIZE + GUIClass.dHeight);
				GUIClass.pan.repaint();
				
				b1.setEnabled(true);
				b2.setEnabled(true);
				b1.setSelected(true);
				GUIClass.pan.setMode(new SettingStartMode());
				b3.setEnabled(false);
				stepToEnd.setEnabled(false);
				//b4.setEnabled(false);
			}
		}
	}
}
